﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using SpatialHashLib.Topology;
using SpatialHashLib.Topology.Indexing;

namespace SpatialHashLib
{
    /// <summary>
    /// Interaction logic for LineSegmentPage.xaml
    /// </summary>
    public partial class LineSegmentPage : UserControl
    {
        #region Auxilliary Classes, Structs, and Enums
        
        class CanvasController
        {
            protected const double DefaultBufferSize = 4;

            Canvas canvas;
            SpatialHash<FrameworkElement> spatialHash;

            Point mouseDown;
            Point mouseMove;
            Point mouseUp;

            protected Point elementPos;
            protected FrameworkElement selectedElement;

            double bufferSize;

            bool mousePressed;

            public CanvasController(Canvas canvas, SpatialHash<FrameworkElement> spatialHash)
            {
                this.canvas = canvas;
                this.spatialHash = spatialHash;
                bufferSize = BufferSize;
            }

            protected SpatialHash<FrameworkElement> SpatialHash { get { return spatialHash; } }

            protected Point MouseDown { get { return mouseDown; } }
            protected Point MouseMove { get { return mouseMove; } }
            protected Point MouseUp { get { return mouseUp; } }

            protected bool MousePressed
            {
                get { return mousePressed; }
            }

            protected virtual double BufferSize
            {
                get { return DefaultBufferSize; }
            }

            public void HandleMouseDown(MouseButtonEventArgs e)
            {
                mouseDown = e.GetPosition(canvas);
                e.Handled = true;
                canvas.CaptureMouse();
                mousePressed = true;
                OnHandleMouseDown(e);
            }

            public void HandleMouseUp(MouseButtonEventArgs e)
            {
                mouseUp = e.GetPosition(canvas);
                e.Handled = true;
                canvas.ReleaseMouseCapture();
                mousePressed = false;
                OnHandleMouseUp(e);
            }

            public void HandleMouseMove(MouseEventArgs e)
            {
                mouseMove = e.GetPosition(canvas);
            }

            protected virtual void OnHandleMouseDown(MouseButtonEventArgs e)
            {
            }

            protected virtual void OnHandleMouseMove(MouseEventArgs e)
            {
            }

            protected virtual void OnHandleMouseUp(MouseButtonEventArgs e)
            {
            }
        }

        class AddController : CanvasController
        {
            public AddController(Canvas canvas, SpatialHash<FrameworkElement> spatialHash)
                : base(canvas, spatialHash)
            {
            }

            protected override void OnHandleMouseDown(MouseButtonEventArgs e)
            {
                
            }
        }

        #endregion

        CanvasController controller;
        SpatialHash<FrameworkElement> spatialHash;

        public LineSegmentPage()
        {
            InitializeComponent();
            spatialHash = new SpatialHash<FrameworkElement>(20, 5);
            controller = new CanvasController(mainCanvas, spatialHash);
        }

        private void add_Checked(object sender, RoutedEventArgs e)
        {
            controller = new AddController(mainCanvas, spatialHash);
        }

        private void edit_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void mainCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            controller.HandleMouseDown(e);
        }

        private void mainCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            controller.HandleMouseMove(e);
        }

        private void mainCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            controller.HandleMouseUp(e);
        }
    }
}
